import axios from 'axios'
import Element from "element-ui";
import store from "./store";
import router from "./router";
// axios.defaults.baseURL = 'http://47.93.247.196:8081'
axios.defaults.baseURL='http://localhost:8081'

axios.interceptors.request.use(config => {
    // 可以统一设置请求头
    return config
})

axios.interceptors.response.use(response => {
    let res = response.data;
    // console.log("后置拦截")
    //
    // console.log("===");
    // console.log(res);
    // console.log("===");

    if (res.code === 200){
        return response
    }else {
        Element.Message.error("错误信息")
        return Promise.reject(res.msg)
    }
},
    error => {

        // console.log(error);
        let errorRes = error.response.data;
        if (errorRes){
            error.message = errorRes.msg
        }

        if (error.response.status === 401){
            store.commit("REMOVE_INFO")
            router.push("/login")
        }
        Element.Message.error(error.message)

    })